﻿
common_guide_control = 
{
};

function common_guide_control:new (game_id, local_cfg_key, record_item, record_sub_item,
            guide_url, guide_report_type, use_server_record,
            need_cloudgame_permission, need_game_permission)
    local common_guide_mode_tmp = common_guide_model:new(game_id, local_cfg_key, record_item,
                                    record_sub_item, guide_url, guide_report_type,
                                    use_server_record, need_cloudgame_permission,
                                    need_game_permission);
    local guide_control = {common_guide_model_ = common_guide_mode_tmp,
    key_ = common_guide_mode_tmp.key_, is_checking_ = false,
    need_cloudgame_permission_ = need_cloudgame_permission,
    need_game_permission_ = need_game_permission};
    setmetatable(guide_control, self);
    self.__index = self;
    guide_control:init();
    self:inner_log("new", "game_id:" .. tostring(game_id)
        .. "local_cfg_key:" .. tostring(local_cfg_key)
        .. "record_item:" .. tostring(record_item)
        .. "record_sub_item:" .. tostring(record_sub_item)
        .. "guide_url:" .. tostring(guide_url)
        .. "guide_report_type:" .. tostring(guide_report_type));
    return guide_control;
end;

function common_guide_control:init()
    self:inner_log("init", "init common guide control");
end;

function common_guide_control:inner_log(msg, core_msg)
    base_utility.log(string.format("[common_guide_control]key:%s, %s", self.key_, msg), core_msg);
end;

function common_guide_control:on_login_success(event_id, json_str)
    local info = base_utility.json_to_table(json_str);
    if self.common_guide_model_ == nil then
        return;
    end;
    if self.common_guide_model_.uin == 0 then
        self.common_guide_model_.uin = info.uin;
    end;
    --[[
    if self.is_checking_ == false then
        self:check_common_guide_record();
    end
    ]]--
end;

function common_guide_control:on_get_account_id(account_id, game_id, no_banner_switch)
    self:inner_log("on_get_account_id:" .. account_id .. ", game_id:" .. tostring(game_id));
    if account_id == nil or account_id == 0 then
        return;
    end;

    if self.common_guide_model_ == nil then
        return;
    end;
    if self.common_guide_model_.uin == 0 then
        self.common_guide_model_.uin = account_id;
    end;
    if self.is_checking_ == false then
        if no_banner_switch == nil or no_banner_switch == false then
            self.common_guide_model_.cur_focus_game_id = game_id;
            self:check_common_guide_record();
        end;
    end
end;


function common_guide_control:check_common_guide_record()
    local local_exist = self:check_local_common_guide_record();
    self:inner_log("check_common_guide_record, local_exist:" .. tostring(local_exist));
    if local_exist == true then
        return;
    end;

    if self.common_guide_model_.use_server_record_ == 0 then
        self.common_guide_model_.guide_has_shown = false;
        if self.is_checking_ == false then
            self.is_checking_ = self:check_should_show_guide();
        end
        return;
    end;

    base_utility.get_accumulate_count(self.common_guide_model_.server_record_item,
        self.common_guide_model_.server_record_sub_item,
        tostring(self.common_guide_model_.uin), "common_guide_control_mgr.on_check_common_guide_record",
        false, self.key_);
end;

function common_guide_control:check_local_common_guide_record(check_show_guide)
    local config = self.common_guide_model_:get_local_common_guide_cfg();
    if nil == config or config.exits == nil or  0 == config.exits then
        self:inner_log("no local record : ");
        if check_show_guide ~= nil and check_show_guide == true then
            self.common_guide_model_.guide_has_shown = false;
            if self.is_checking_ == false then
                self.is_checking_ = self:check_should_show_guide();
            end
        end;
        return false;
    else
        self:inner_log("has local record : "..tostring(config.value));
        return true;
    end;
end;

function common_guide_control:on_check_common_guide_record(json_str)
    self:inner_log("on_check_common_guide_record : "..json_str);
    if json_str == nil then
            self:check_local_common_guide_record(true);
        return;
    end
    local result_tb = base_utility.json_to_table(json_str);
    if result_tb == nil or result_tb.result == nil then
        self:check_local_common_guide_record(true);
        return;
    end

    if 0 == result_tb.result then
        local list = result_tb.sub_item_list;
        local query_sub_item = false;
        if list == nil or type(list) ~= "table" or nil == next(list) then  
            self.common_guide_model_.guide_has_shown = false;
        else
            local item_count = #list;
            self:inner_log("on_check_common_guide_record : item_count"
                ..tostring(item_count));
            for id = 1, item_count do
                local sub_item_list = list[id];
                if sub_item_list ~=nil and sub_item_list.sub_item ~= nil and
                    sub_item_list.sub_item == self.common_guide_model_.server_record_sub_item then
                    if sub_item_list.is_reported == nil or sub_item_list.is_reported == 0 then
                        self.common_guide_model_.guide_has_shown = false;
                    else
                        self.common_guide_model_.guide_has_shown = true;
                    end;
                    query_sub_item = true;
                    break;
                end;
            end;
        end;

        if query_sub_item == false then
            self.common_guide_model_.guide_has_shown = false;
        end;
        if self.is_checking_ == false then
            self.is_checking_ = self:check_should_show_guide();
        end
        return;
    end
    self.is_checking_ = false;
end;

function common_guide_control:on_set_server_common_guide_cfg(json_str)
    self.common_guide_model_:on_set_server_common_guide_cfg(json_str)
end;

function common_guide_control:check_should_show_guide()
    --  开发者版不弹
    if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
        return false;
    end

    --  开发者版不弹
    if true == base_utility.is_develop_client() then
        return false;
    end

    -- 已经查询到是否需要显示引导
    if self.common_guide_model_.guide_has_shown == nil or 
        self.common_guide_model_.guide_has_shown == true then
        self:inner_log("guide has shown, no need show");
        return false;
    end

    if self.common_guide_model_.need_cloudgame_permission_ ~= nil and 
        self.common_guide_model_.need_cloudgame_permission_ ~= 0 and
        self.common_guide_model_.get_cloud_game_permission_ == false then
        self:inner_log("guide has shown, need get cloud game persion to show, game_id:"
        .. tostring(self.common_guide_model_.show_entry_game_id));
        return false;
    end

    if self.common_guide_model_.need_game_permission_ ~= nil and 
        self.common_guide_model_.need_game_permission_ ~= 0 and
        self.common_guide_model_.get_game_permission_ == false then
        self:inner_log("guide has shown, need get game persion to show, game_id:"
        .. tostring(self.common_guide_model_.show_entry_game_id));
        return false;
    end

    if self.common_guide_model_.show_entry_game_id == nil or 
        self.common_guide_model_.cur_focus_game_id == nil or
        self.common_guide_model_.show_entry_game_id ~= self.common_guide_model_.cur_focus_game_id then
        self:inner_log("cur focus game id not equal common guide id");
        return false;
    end

    self:inner_log("show_entry_game_id:"
    .. tostring(self.common_guide_model_.show_entry_game_id)
    .. ",cur_focus_game_id:" .. tostring(self.common_guide_model_.cur_focus_game_id));
    self:show_common_guide_guide();
    return true;
end;

function common_guide_control:change_focus_game(game_id)
    if game_id == nil or game_id == 0 then
        return;
    end
    self.common_guide_model_.cur_focus_game_id = game_id;
end;

function common_guide_control:handle_banner_switch(event_id, json_str, no_banner_switch)
    self:inner_log("handle_banner_switch:" .. json_str);
    if json_str == nil then
        return;
    end
    local params = base_utility.json_to_table(json_str);
    local game_id = nil;
    if params ~= nil then
        if params.module_id ~= nil then
            game_id = tonumber(params.module_id);
        end
    end
      
    if game_id == nil then
        return;
    end
    if self.is_checking_ == false then
        if no_banner_switch == nil or no_banner_switch == false then
            self.common_guide_model_.cur_focus_game_id = game_id;
            self.is_checking_ = self:check_should_show_guide();
        end;
    end
end;

function common_guide_control:on_close_main_wnd(data)
end;

function common_guide_control:show_common_guide_guide()
    self:inner_log("show common guide guide");
    wegame_mainframe_view.set_pack_up_btn_visible(false);
    local params = {
        url = self.common_guide_model_.guide_url_,--;LoadStr("CLOUD_GAMING_GUIDE_URL"),
        report_type = self.common_guide_model_.guide_report_type_,--"yunyouxi_guide",
        need_focus_home = false,
        guide_show_callback = function()
            self.common_guide_model_.guide_has_shown = true;
            self.is_checking_ = false;
            self.common_guide_model_:set_server_common_guide_cfg();
            return;
        end,
        guide_close_callback = function()
            self.is_checking_ = false;
            return;
        end,
    };
    simple_user_guide_control.show_custom_user_guide(params);
end;

function common_guide_control:on_cloud_game_cfg_info_brc(game_id)
    if game_id == nil or game_id == 0 then
        return;
    end
    self.common_guide_model_.get_cloud_game_permission_ = true;
    self:inner_log("on_cloud_game_cfg_info_brc get game_id:" .. tostring(game_id));
    if self.is_checking_ == false then
        self.is_checking_ = self:check_should_show_guide();
    end
end;
